[assembly: Rage.Attributes.Plugin("OC Spray", Author = "Rinion", Description = "This plugin adds OC Spray effects")]

namespace OCSpray3
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Rage;

    //If ped is hit by oc spray play sound and animation and incapacitate ped for a while.

    internal static class EntryPoint
    {

        private static void Main()
        {
            Game.LogTrivial("OC Spray Plugin has loaded successfully");

            var ocSpray = new WeaponAsset("weapon_ocspray");

            Ped shotPed = null;

            GameFiber.StartNew(delegate
            {
            start:
                while (true)
                {

                    Entity Targeted = Game.LocalPlayer.GetFreeAimingTarget();
                    Vector3 start = Game.LocalPlayer.Character.Position;
                    Vector3 end = start + Game.LocalPlayer.Character.Direction * 3f;

                    GameFiber.Yield();

                    if (Game.LocalPlayer.Character.Inventory.EquippedWeapon != null)
                    {
                        var equipped = new WeaponAsset(Game.LocalPlayer.Character.Inventory.EquippedWeapon.Asset);

                        HitResult rayResult = World.TraceCapsule(start, end, 4f, TraceFlags.IntersectPeds | TraceFlags.IntersectVehicles, Game.LocalPlayer.Character);

                        if (Targeted is Ped && rayResult.HitEntity == Targeted)
                        {
                            shotPed = (Ped)Game.LocalPlayer.GetFreeAimingTarget();
                        }
                        else
                        {
                            goto start;
                        }

                        if (shotPed is Ped && Game.LocalPlayer.Character.IsShooting && ocSpray == equipped)
                        {
                            shotPed.Tasks.Clear();

                            shotPed.PlayAmbientSpeech("waveload_pain_female", "COUGH", 28, SpeechModifier.Standard);
                            shotPed.Tasks.PlayAnimation("missminuteman_1ig_2", "tasered_1", 1, AnimationFlags.None);
                        }
                        else
                        {
                            goto start;
                        }

                    }
                    else
                    {
                        goto start;
                    }
                }
            });
        }
    }
}
